<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <h3>事件监听</h3>
    <p id="text">为 DOM 元素添加事件监听，等待事件发生，便立即执行一个函数。</p>
    <button id="btn">点击改变文字颜色</button>
    <script>
        const btn = document.querySelector('#btn')
        btn.addEventListener('click', () => {
            console.log('等待事件被触发...')
            let text = document.getElementById('text')
            if (text.style.color != 'green') {
                text.style.color = 'green'
                text.style.fontSize='20px'
            } else {
                text.style.color = 'red'
            }
        })


    </script>

</body>

</html>